# -*- coding: utf-8 -*-
"""
@Time ： 2022/12/12 20:03
@Author ： ZhengYunYi
@File ：TaoBaoComment.py
@IDE ：PyCharm

"""
import time

from selenium import webdriver
from selenium.webdriver.common.by import By  # 查找元素
from selenium.webdriver.support.ui import WebDriverWait  # 等待元素出现

if __name__ == '__main__':
    USER_DIR = r"C:\Users\Administrator\AppData\Local\Google\Chrome\User Data"
    option = webdriver.ChromeOptions()
    option.add_argument("--user-data-dir={}".format(USER_DIR))
    # 绕过登录反爬机制
    # option.add_argument("--headless")
    # 无头浏览器需要添加user-agent来隐藏特征
    option.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                        'Chrome/94.0.4606.71 Safari/537.36')
    driver = webdriver.Chrome(options=option)
    driver.implicitly_wait(1)
    with open('Files/stealth.min.js') as f:
        js = f.read()
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": js
    })
    url = 'https://item.taobao.com/item.htm?spm=a230r.1.14.34.42102140MiVD5P&id=686060610562&ns=1&abbucket=19'
    driver.get(url)
    time.sleep(100)
    a = WebDriverWait(driver, timeout=3).until(lambda d: d.find_element(By.LINK_TEXT, r'评  价'))
    a.click()
    b = WebDriverWait(driver, timeout=3).until(lambda d: d.find_elements(By.CLASS_NAME, 'J_KgRate_ReviewItem kg-rate-ct-review-item'))
    print(b)
    pass
